package com.tg.appcommon.helper;

import android.app.Activity;

import com.appbase.custom.app.AppLoginStatusChangeListener;
import com.appbase.custom.app.LOGIN_STATUS;
import com.appbase.custom.constant.CommonConstants;
import com.module.appcommon.R;
import com.tange.base.toolkit.PreferenceUtil;
import com.tg.appcommon.android.ResourcesUtil;
import com.tg.appcommon.android.TGAlertDialog;
import com.tg.appcommon.android.TGApplicationBase;
import com.tg.appcommon.android.TGLog;
import com.tange.base.toolkit.TGThreadPool;
import com.tg.appcommon.business.IAppModule;
import com.tg.appcommon.router.TGBusiness;
import com.tg.appcommon.sdk.TGMessageType;
public class LogoutDialogHelper  implements AppLoginStatusChangeListener {
    private static final String TAG = LogoutDialogHelper.class.getSimpleName();
    private TGAlertDialog logoutDialog;
    private boolean mMustDialog = false;
    private String  mDiaLogParam;

    @Override
    public void onLoginStatusChange(int status) {
        if (status == LOGIN_STATUS.STATUS_LOGOUT){
            if (!LogoutDialogHelper.getInstance().isDialogShow()){
                LogoutDialogHelper.getInstance().showLogoutDialog(null);
            }
        }
    }

    public static LogoutDialogHelper getInstance() {
        return LogoutDialogHelper.SingletonHolder.sInstance;
    }

    private static class SingletonHolder {
        private static final LogoutDialogHelper sInstance = new LogoutDialogHelper();
    }

    public String getDiaLogParam() {
        return mDiaLogParam;
    }


    public boolean isMustDialog() {
        return mMustDialog;
    }

    public boolean isDialogShow() {
        return logoutDialog != null && logoutDialog.isShowing();
    }

    public void showLogoutDialogImpl(String time) {
        TGLog.d(TAG, "showLogoutDialog run isBackGround = " + TGApplicationBase.isBackGround());
        IAppModule iAppModule = TGBusiness.getAppModule();
        iAppModule.removeAllCamera();



        if (!TGApplicationBase.isBackGround()) {
            mMustDialog = false;
            mDiaLogParam = null;
            if (logoutDialog != null) {
                logoutDialog.cancel();
                logoutDialog = null;
            }
            int  sure= R.string.confirm;
            String message = ResourcesUtil.getString(R.string.user_has_logged_out);
            Activity activity = TGApplicationBase.getInstance().getNoFinishActivity();
            if (activity == null) {
                mMustDialog = true;
                logoutDialog = null;
                return;
            }
            logoutDialog = new TGAlertDialog(activity).builder();
            logoutDialog.setTitle(message);
            logoutDialog.setNegativeButton(sure, v -> {
                        PreferenceUtil.setBoolean(TGApplicationBase.getApplication().getBaseContext(), CommonConstants.PRE_AUTO_LOGIN, false);
                        PreferenceUtil.setBoolean(TGApplicationBase.getApplication().getBaseContext(), CommonConstants.PRE_AUTO_WECHAT_LOGIN, false);
                        PreferenceUtil.setString(TGApplicationBase.getApplication().getBaseContext(), CommonConstants.PRE_TOKEN, "");
                        iAppModule.startLoginActivity(TGApplicationBase.getApplication().getBaseContext(), "start_logout");
                        logoutDialog = null;
                    });

            logoutDialog.setCancelable(false).show();
        } else {
            mMustDialog = true;
            mDiaLogParam = time;
        }
    }


    public void showLogoutDialog(final String time) {
        TGLog.trace("showLogoutDialog");
        TGThreadPool.executeOnUiThread(() -> LogoutDialogHelper.getInstance().showLogoutDialogImpl(time));
    }

}
